// pages/addressAdd/adressAdd.js
const fetchSev = require('../../utils/util.js').fetchSev;
const addAddressUrl = require('../../config.js').addAddressUrl;
const delAddressUrl = require('../../config.js').delAddressUrl;

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    skipType: '',   // 添加？编辑？
    receiver: '',   // 收件人
    phone: '',      // 手机号
    address: '',    // 地址
    status: 0,      // 0-默认  1-普通
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 新增？ 编辑？
    const type = options.type;
    this.setData({skipType: type});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log();
    const type = this.data.skipType;
    
    
    if (type == 'add') {      
      this.setData({ receiver: '' });
      this.setData({ phone: '' });
      this.setData({ address: '' });
      this.setData({ status: 0 });  
    } else if (type == 'edit') {
      // 当前被编辑的一条地址
      const curAddress = app.globalData.curAddress;
      console.log(curAddress);
      this.setData({ receiver: curAddress.receiver });
      this.setData({ phone: curAddress.phone });
      this.setData({ address: curAddress.address });
      this.setData({ status: curAddress.status });    
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  // 用户输入
  _inputChange: function(e) {
    console.log(e)
    const type = e.currentTarget.dataset.type;
    const value = e.detail.value;
    switch(type){
      case 'receiver':
        this.setData({ receiver: value});
        break;
      case 'phone':
        this.setData({ phone: value });
        break;
      case 'address':
        this.setData({ address: value });
        break;
    }
  },


  // 默认地址切换
  _switchChange: function(e) {
    const toggle = e.detail.value;
    toggle ? this.setData({status: 0}) : this.setData({status: 1});
  },


  // 提交
  _submit: function() {
    let data = null;
    if (this.data.skipType == 'add') {   // 添加
      data = {
        receiver: this.data.receiver,
        phone: this.data.phone,
        address: this.data.address,
        status: this.data.status
      };
    } else {   // 修改
      data = {
        receiver: this.data.receiver,
        phone: this.data.phone,
        address: this.data.address,
        status: this.data.status,
        id: app.globalData.curAddress.id
      };
    }
    
    // 数据校验
    if (!data.receiver) {
      wx.showToast({
        title: '收件人不能为空',
        icon: 'none'
      });
      return;
    }
    if (!data.phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      });
      return;
    }
    if (!data.address) {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none'
      });
      return;
    }
    // 手机正则验证
    const phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (!phoneReg.test(data.phone)){
      wx.showToast({
        title: '手机号有误，请重新输入',
        icon: 'none'
      });
      return;
    }
    console.log('提交-data', data);
    fetchSev(addAddressUrl, 'POST', data, (res)=>{
      wx.showToast({
        title: '成功',
        success: function() {
          wx.navigateBack({
            delta: 1
          })
        }
      });
      
    })
  },

  // 删除地址
  _delete: function() {
    const id = app.globalData.curAddress.id;
    if (id) {
      fetchSev(delAddressUrl, 'GET', {id: id}, (res)=>{
        wx.showToast({
          title: '删除成功',
          success: function() {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      })
    }
   }
})